package figure8.GUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import figure8.Controller;
import figure8.LoopTiming;
import figure8.Project;
import figure8.Seq;

public class SeqAlreadyExistsWindow extends JDialog {
	public SeqAlreadyExistsWindow(final SeqMainWindow window, final File directory, final String name, final Seq seq)
	{
		this.setLocation(window.getPanel().getX() + 80, window.getPanel().getY() + 120);
		
		final JDialog warningWindow = this;	
		JPanel panel = new JPanel(new GridLayout(3,1));
		panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		this.getContentPane().add(panel);
		
		panel.add(new JLabel("A sequencer with this name already exists."));
		panel.add(new JLabel("It will be overwritten. Are you sure you wish to do this?"));

		JPanel buttons = new JPanel(new GridLayout(1,2));
		JButton yes = new JButton("Yes");
		yes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					File seqDir = new File(directory + File.separator + name);
					File[] filesInDir = seqDir.listFiles();
					for(File fileFound : filesInDir) 
					{
						if(fileFound.delete() == false) {
								System.err.println(fileFound.getAbsolutePath() + " failed to delete.");
						}
					}
					seq.saveFile(seqDir);
				} catch(Exception e) {
					e.printStackTrace();
				}
				warningWindow.dispose();
			}
		});
		buttons.add(yes);
		JButton no = new JButton("No");
		no.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				warningWindow.dispose();
			}
		});
		buttons.add(no);
		panel.add(buttons);

		this.pack();
		this.setVisible(true);
	}
}